﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CoreMobile.bce;
using ERIMobileL.oo;
using ERIMobileL.ui;
using Opticon.Bcrctl;
using CoreMobile.cfg;
using ERIMobileL.cfg;

namespace ERIMobileL.ui
{
    public partial class ERIForm : Form, IBCENotifiable
    {

        private List<Event<int>> events;
        private Event<int> currentEvent;
        private BCEReader reader;


        public ERIForm()
        {
            InitializeComponent();
            events=new List<Event<int>>();
            reader = BCEReader.getReaderWith(this);
            currentEvent = new Event<int>();
            this.eventCodes.Items.Clear();
            foreach(var code in SettingsManager<ELSettings>.get().Settings.Codes)
            {
                eventCodes.Items.Add(code);
            }
            //resetUI();
        }

        private void register_Click(object sender, EventArgs e)
        {
           

            if( eventCodes.SelectedIndex < 0)
            {
                MessageBox.Show("Debe selecionar un evento");
                return;
            }
            if(string.IsNullOrEmpty(rut.Text))
            {
                MessageBox.Show("Debe leer el CI del receptor");
                return;
            }
            
            currentEvent.Code = eventCodes.SelectedItem.ToString();
            events.Add(currentEvent);
            currentEvent.setNow();
            
            currentEvent = new Event<int>();
            resetUI();
        }

        private void resetUI()
        {
            rut.Text = "";
            items.Items.Clear();
        }

        private void existItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void viewRegistries_Click(object sender, EventArgs e)
        {
            //view registries!!!
            var list=new ListForm(events);

            list.ShowDialog();
            /*if (DialogResult.OK == list.ShowDialog())
            {

            }*/
        }

        public void receiverDataFromReader(object sender, BarcodeDecoder.ScanEventArgs e)
        {
            string data = reader.preProcess(e);

            if (string.IsNullOrEmpty(data)) return;

            if(e.CodeType == BarcodeDecoder.CodeIdTypes.PDF417)
            {
                registerPerson(BCEReader.decodePDF417RegCivil(data)[0]);
                return;
            }
            registerItem(data);
        }

        private void registerItem(string codeItem)
        {
            if (currentEvent.Items.ContainsKey(codeItem))
            {
                currentEvent.Items[codeItem] += 1;
                int idx=getListViewItemIdx(codeItem);
                items.Items[idx].SubItems[1].Text = currentEvent.Items[codeItem].ToString();
                return;
            }
            currentEvent.Items.Add(codeItem, 1);
            items.Items.Add(new ListViewItem(new string[] {codeItem, "1"}));
            items.Refresh();
        }

        private int getListViewItemIdx(string codeItem)
        {
            for (int i = 0; i < items.Items.Count; i++)
            
               if(items.Items[i].Text == codeItem) return i;
           
            throw new ArgumentOutOfRangeException("Code ["+codeItem+"] doesnt exist");
        }

        private void registerPerson(string rut)
        {
           currentEvent.Actor = new Person(rut);
            this.rut.Text = currentEvent.Actor.ppRut();
        }

    }
}